﻿using Oracle.ManagedDataAccess.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Core.Extensions;
using VOL.Domain.Infrastructure;
using VOL.Domain.IServices.WarehouseManagement;
using VOL.Entity.DomainModels;

namespace VOL.Domain.Services.WarehouseManagement
{
    public class InboundProductsService: IInboundProductsService
    {
        private readonly IBaseRepository<InboundProducts> _inboundProductsRepository;

        public InboundProductsService(IBaseRepository<InboundProducts> inboundProductsRepository)
        {
            _inboundProductsRepository = inboundProductsRepository;
        }
        /// <summary>
        /// 产品入库新增
        /// </summary>
        /// <param name="inboundProducts"></param>
        /// <returns></returns>
        public async Task<int> AddInboundProducts(InboundProducts inboundProducts)
        {
            return await _inboundProductsRepository.Add(inboundProducts);
        }
        /// <summary>
        /// 产品入库显示
        /// </summary>
        /// <returns></returns>
        public async Task<List<InboundProducts>> GetAllInboundProducts(string? inboundCode, string? inboundName, string? workCode, int pageIndex = 1, int pageSize = 5)
        {
            var inboundProductList = _inboundProductsRepository.GetAll();
            inboundProductList = inboundProductList.WhereIF(inboundCode != null, m => m.InboundCode.Contains(inboundCode));
            inboundProductList = inboundProductList.WhereIF(inboundName != null, m => m.InboundName.Contains(inboundName));
            inboundProductList = inboundProductList.WhereIF(workCode != null, m => m.WorkCode.Contains(workCode));
            var total = inboundProductList.Count();
            var items = inboundProductList.OrderBy(m => m.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return inboundProductList.ToList();
        }
        /// <summary>
        /// 产品入库修改
        /// </summary>
        /// <param name="inboundProducts"></param>
        /// <returns></returns>
        public async Task<int> UpdateInboundProducts(InboundProducts inboundProducts)
        {
            return await _inboundProductsRepository.Update(inboundProducts);
        }
        /// <summary>
        /// 产品入库删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteInboundProducts(int id)
        {
            var inboundProduct = (await _inboundProductsRepository.FindAsync(m => m.Id == id)).FirstOrDefault();
            if (inboundProduct == null) return 0;
            return await _inboundProductsRepository.Delete(inboundProduct);
        }
    }
}
